package com.example.androidsx;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class ProductDetailActivity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail1);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        String productName = getIntent().getStringExtra("product_name");
        String productPrice = getIntent().getStringExtra("product_price");
        TextView title = findViewById(R.id.toolbar_title);
        title.setText(productName != null ? productName : "商品详情");

        ImageButton btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> onBackPressed());

        TextView productDetail = findViewById(R.id.product_detail);
        if (productName != null && productPrice != null) {
            productDetail.setText(String.format(
                    "商品名称：%s\n\n价格：%s\n\n库存：100件\n\n描述：高端优质商品，品质保证，健康安全，产地直销，新鲜直达，品质保障！",
                    productName, productPrice
            ));
        }

        setupBottomButtons();
    }

    private void setupBottomButtons() {
        Button btnHome = findViewById(R.id.btn_home);
        btnHome.setOnClickListener(v -> {
            Intent intent = new Intent(this, ShopListActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        });

        Button btnCart = findViewById(R.id.btn_cart);
        btnCart.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(ProductDetailActivity1.this, v);
            popup.getMenuInflater().inflate(R.menu.cart_menu, popup.getMenu());
            popup.show();
        });

        Button btnProfile = findViewById(R.id.btn_profile);
        btnProfile.setOnClickListener(v -> {
            startActivity(new Intent(this, ProfileActivity.class));
        });

        Button btnCheckout = findViewById(R.id.btn_checkout);
        btnCheckout.setOnClickListener(v -> {
            startActivity(new Intent(this, CheckoutActivity.class));
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class ProfileActivity {
    }
}